﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;
using WindowsGame1.Extensions;
namespace WindowsGame1
{
    public class DungeonGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        Texture2D smhfl;
        SpriteFont font;
        Input _player1 = new Input(PlayerIndex.One);
        private readonly Random _rand;

        public DungeonGame(int? seed=null)
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            if (seed.HasValue)
                _rand = new Random(seed.Value);
            else
                _rand = new Random();
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }
        bool _showMenus = true;
        private void ProcessUpdates(GameTime gameTime)
        {
            if (this._showMenus)
            {
                ProcessMenus(gameTime);
                return;
            }
            else
            {
                ProcessGame(gameTime);
                return;
            }

        }

        Microsoft.Xna.Framework.Rectangle GetHitBox(Vector2 position, int width, int height)
        {
            return new Rectangle((int)position.X, (int)position.Y, width, height);
        }

        private RectDungeon dungeon;
        private void ProcessMenus(GameTime gameTime)
        {
            IsMouseVisible = true;
            if(_player1.Activate)
            {
                _showMenus = false;
                if (dungeon == null)
                    dungeon=InitializeDungeon(_rand);
            }
        }
        
        /// <remarks>
        /// reference http://roguebasin.roguelikedevelopment.org/index.php?title=Basic_BSP_Dungeon_generation
        /// </remarks>
        public static RectDungeon InitializeDungeon(Random rnd)
        {
          var dungeon = new RectDungeon(0, 0, 0, 100, 100);
            var didSplit=dungeon.split();
            Debug.Assert(didSplit);
            var current = dungeon;
            while(didSplit)
            {

                didSplit = current.split() || (current.LeftChild!=null && current.LeftChild.split()) || (current.RightChild!=null&&current.RightChild.split());
                current = rnd.NextBool() ? current.LeftChild : current.RightChild;
            }
            dungeon.generateDungeon();
            return dungeon;
        }
        private void ProcessGame(GameTime gameTime)
        {
            if (_player1.Pause)
            {
                _showMenus = true;
                return;
            }
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (_player1.Quit)
                this.Exit();
            else
                ProcessUpdates(gameTime);


            base.Update(gameTime);
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            smhfl = Content.Load<Texture2D>("smhfl");
            font = Content.Load<SpriteFont>("Fire");
        }
        SpriteBatch spriteBatch;
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            int resolutionWidth = graphics.GraphicsDevice.Viewport.Width;
            int resolutionHeight = graphics.GraphicsDevice.Viewport.Height;

            if (_showMenus)
            {
                DrawMenus();
            }
            else
            {
                DrawDungeon(resolutionHeight, resolutionWidth);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void DrawMenus()
        {
           
        }

        private void DrawDungeon(int resolutionHeight, int resolutionWidth)
        {
            spriteBatch.DrawString(font, dungeon.ToString(),new Vector2(),Color.Violet);
        }
    }
}
